define(['underscore','backbone','jquery','views/widget','text!templates/text.html'], function(_,Backbone,$,ViewWidget,TextTemplate){
    var PureHTMLView = ViewWidget.extend({
        template : _.template(TextTemplate),
        initialize : function(){
            this.model.bind('change', this.render, this);
            this.model.view = this;
        },
        render: function() {
            $(this.el).html(this.template(this.model.toJSON()));
            this.edit();
            return this;
        },
        edit : function()
        {
            if (this.content_editor == undefined)
            {
                this.content_editor = CodeMirror.fromTextArea(this.$('textarea').get(0), {
                    mode: 'text/html',
                    tabMode: "indent",
                    lineNumbers: true,
                    lineWrapping: true,
                    onHighlightComplete : $.proxy(function(){this.content_editor.refresh();},this),
                    onCursorActivity: $.proxy(function() {
                        this.content_editor.setLineClass(hlLine, null, null);
                        hlLine = this.content_editor.setLineClass(this.content_editor.getCursor().line, null, "activeline");
                    },this),
                    autofocus : true
                });
                var hlLine = this.content_editor.setLineClass(0, "activeline");
            }
            ViewWidget.prototype.edit.call(this);
            return false;
        },
        save : function()
        {
            Backbone.emulateJSON = true;
            this.content_editor.save();
            this.content_editor = null;
            ViewWidget.prototype.save.call(this);
            return false;
        },
        cancel : function(){
            this.content_editor = null;
            ViewWidget.prototype.cancel.call(this);
        }
    });
    return PureHTMLView;
});